package manage.actions.role;

import java.util.Map;

import manage.actions.ManageAction;
import manage.commons.annontations.AccessControl;
import manage.commons.annontations.AccessVerify;
import manage.commons.annontations.RequiredLogin;
import manage.commons.utils.CommonUtil;
import manage.commons.utils.InputStreamUtil;
import manage.commons.utils.Pager;
import manage.modules.role.model.Role;
import manage.modules.role.service.RoleService;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

@RequiredLogin
@AccessVerify
public class RoleAction extends ManageAction {

	private static final long serialVersionUID = -5077065854791413816L;
	private RoleService roleService;
	private Pager<Role> pager = new Pager<Role>(100);
	private Pager<Map<String, Object>> workerPager = new Pager<Map<String, Object>>(
			500);
	private Role role;
	private String workerIds;
	private Map<String, String> param;

	@Override
	@AccessControl
	@Action("role-input")
	public String input() {
		return SUCCESS;
	}

	@AccessVerify(-1)
	@Action(value = "role-addorupdate", results = { @Result(name = SUCCESS, location = "/role/role-form.jsp") })
	public String forwardAddOrUpdatePage() {
		if (isAddOperater()) {
			action = "/role/role-add.do";
			getRequest().setAttribute("btnKeyWord", "添加角色");
		} else if (isUpdateOperater()) {
			action = "/role/role-edit.do";
			getRequest().setAttribute("btnKeyWord", "更新角色");
			role = roleService.select(role.getRoleId());
		}
		return SUCCESS;
	}

	@AccessControl
	@Action(value = "role-query", results = { @Result(name = SUCCESS, location = "/role/role-list.jsp") })
	public String query() throws Exception {
		if (role != null) {
			roleService.query(role, pager);
			pager.pagerUtils(pager, getRequest());
		}
		return SUCCESS;
	}

	@AccessVerify(-1)
	@Action(value = "role-select", results = { @Result(name = SUCCESS, type = "json", params = {
			"root", "role" }) })
	public String select() {
		if (null != role && null != role.getRoleId()) {
			boolean exists = roleService.exists(role.getRoleId());
			if (exists) {
				role = roleService.select(role.getRoleId());
			}
		}
		return SUCCESS;
	}

	@Action(value = "role-add", results = { @Result(name = SUCCESS, type = "stream", params = {
			"contentType", "text/plain; charset=utf-8", "inputName",
			"inputStream" }) })
	public String add() throws Exception {
		if (null != role && role.getRoleName() != null
				&& null != role.getOrderNo()) {
			int count = roleService.insert(role);
			if (count == 1) {
				inputStream = InputStreamUtil.getSuccessInputStream();
			} else {
				inputStream = InputStreamUtil.getFailedInputStream();
			}
		}
		return SUCCESS;
	}

	@Action(value = "role-delete", results = { @Result(name = SUCCESS, type = "stream", params = {
			"contentType", "text/plain; charset=utf-8", "inputName",
			"inputStream" }) })
	public String delete() throws Exception {
		if (null != role && null != role.getRoleId()) {
			boolean exists = roleService.exists(role.getRoleId());
			if (exists) {
				int delNum = roleService.delete(role.getRoleId());
				if (delNum >= 1) {
					inputStream = InputStreamUtil.getSuccessInputStream();
				} else {
					inputStream = InputStreamUtil.getFailedInputStream();
				}
			} else {
				inputStream = InputStreamUtil.getNotExistsInputStream();
			}
		}
		return SUCCESS;
	}

	@Action(value = "role-edit", results = { @Result(name = SUCCESS, type = "stream", params = {
			"contentType", "text/plain; charset=utf-8", "inputName",
			"inputStream" }) })
	public String edit() throws Exception {
		if (null != role && null != role.getRoleId()
				&& role.getRoleName() != null && null != role.getOrderNo()) {
			boolean exists = roleService.exists(role.getRoleId());
			if (exists) {
				int count = roleService.update(role);
				if (count >= 1) {
					inputStream = InputStreamUtil.getSuccessInputStream();
				} else {
					inputStream = InputStreamUtil.getNoChangeInputStream();
				}
			} else {
				inputStream = InputStreamUtil.getNotExistsInputStream();
			}
		} else {
			inputStream = InputStreamUtil.getFailedInputStream();
		}
		return SUCCESS;
	}

	/**
	 * 员工工分配角色入口
	 * 
	 * @return
	 */
	@Action("worker-role-input")
	@AccessVerify(-1)
	@AccessControl
	public String workerRoleInput() {
		role = roleService.select(role.getRoleId());
		request.setAttribute("roleOfWorker",
				roleService.listWorkerbyRoleId(role.getRoleId()));
		return SUCCESS;
	}

	@Action(value = "worker-role-setting")
	@AccessVerify(-1)
	public String workerRoleForward() {
		role = roleService.select(role.getRoleId());
		if (StringUtils.equals("d", super.getParameterForString("o"))) {
			super.setAttribute("type", 0);// 删除
		} else {
			super.setAttribute("type", 1);// 新增
		}
		return SUCCESS;
	}

	/**
	 * 分工分配角色入口
	 * 
	 * @return
	 */
	@Action(value = "worker-role-query", results = { @Result(name = SUCCESS, location = "/role/worker-role-list.jsp") })
	@AccessVerify(-1)
	@AccessControl
	public String workerRoleQuery() {
		if (StringUtils.equals("0", param.get("type"))) {// 删除
			super.setAttribute("actionUri", "/role/worker-role-delete.do");
		} else if (StringUtils.equals("1", param.get("type"))) {// 新增
			super.setAttribute("actionUri", "/role/worker-role-add.do");
		}
		Integer roleId = NumberUtils.toInt(param.get("roleId"));
		role = roleService.select(roleId);
		roleService.query(param, workerPager);
		request.setAttribute("pagerName", "workerPager");
		return SUCCESS;
	}

	/**
	 * 角色增加员工
	 * 
	 * @return
	 */
	@Action(value = "worker-role-add", results = { @Result(name = SUCCESS, type = "stream", params = {
			"contentType", "text/plain; charset=utf-8", "inputName",
			"inputStream" }) })
	public String workerRoleAdd() {
		role = roleService.select(role.getRoleId());
		if (StringUtils.isNotEmpty(workerIds)) {
			String[] wIds = CommonUtil.evalStringToArray(workerIds);
			roleService.addWorkerToRole(role.getRoleId(),
					CommonUtil.evalLongArray(wIds));
			inputStream = InputStreamUtil.toInputStream("员工角色分配成功！");
		} else {
			inputStream = InputStreamUtil.toInputStream("员工角色分配失败！");
		}
		return SUCCESS;
	}

	/**
	 * 角色删除员工
	 * 
	 * @return
	 */
	@Action(value = "worker-role-delete", results = { @Result(name = SUCCESS, type = "stream", params = {
			"contentType", "text/plain; charset=utf-8", "inputName",
			"inputStream" }) })
	public String workerRoleDelete() {
		role = roleService.select(role.getRoleId());
		if (StringUtils.isNotEmpty(workerIds)) {
			String[] wIds = CommonUtil.evalStringToArray(workerIds);
			roleService.deleteWorkerFromRole(role.getRoleId(),
					CommonUtil.evalLongArray(wIds));
		}
		inputStream = InputStreamUtil.toInputStream("员工角色删除成功！");
		return SUCCESS;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public Pager<Role> getPager() {
		return pager;
	}

	public void setPager(Pager<Role> pager) {
		this.pager = pager;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public Map<String, String> getParam() {
		return param;
	}

	public void setParam(Map<String, String> param) {
		this.param = param;
	}

	public Pager<Map<String, Object>> getWorkerPager() {
		return workerPager;
	}

	public void setWorkerIds(String workerIds) {
		this.workerIds = workerIds;
	}

}
